package com.itmk.system.common.controller;


import com.google.code.kaptcha.Producer;
import com.itmk.business.images.service.QiniuService;
import com.itmk.constants.Constant;
import com.itmk.result.DataResult;
import com.itmk.system.common.entity.JsonData;
import com.itmk.system.common.service.Impl.PublicServiceImpl;
import com.itmk.system.common.service.RedisService;
import com.itmk.system.permission.service.PermissionService;
import com.itmk.system.user.service.UserService;
import com.itmk.utils.JwtTokenUtil;
import com.itmk.vo.req.LoginReqVO;
import com.itmk.vo.resp.LoginRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags="公共接口",description="是所有公共接口的入口")
@RequestMapping("/api/pub")
public class PublicController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private Producer kaptchaProducer;

    @Autowired
    private PublicServiceImpl publicService;

    @Autowired
    private QiniuService qiniuService;

    @RequestMapping("need_login")
    public JsonData needLogin() {

        return JsonData.buildSuccess("温馨提示：请使用对应的账号登录", -2);

    }


    @RequestMapping("not_permit")
    public JsonData notPermit() {

        return JsonData.buildSuccess("温馨提示：拒绝访问，没权限", -3);
    }


    /**
     * 登录接口
     *
     * @param vo
     * @return
     */
    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public DataResult<LoginRespVO> login(@Valid @RequestBody LoginReqVO vo){
        DataResult<LoginRespVO> dataResult=DataResult.success();
        dataResult.setData(publicService.loginin(vo));
        return dataResult;
    }

    @ApiOperation(value = "退出登录接口")
    @PostMapping("/logout")
    public DataResult login(HttpServletRequest request){
        String accessToken=request.getHeader(Constant.ACCESS_TOKEN);
        String userId= JwtTokenUtil.getUserId(accessToken);
        String redisKey= Constant.HOME_REDIS_KEY +"_"+userId.toString();
//        先从缓存里找如果有返回
        if(redisService.hasKey(redisKey)){
            redisService.delete(redisKey);
        }
        return DataResult.getResult(0,"退出成功");
    }


    @ApiOperation(value = "验证码接口")
    @GetMapping("/kaptcha")
    public void getKaptcha(HttpServletResponse response, HttpSession session) throws IOException {
        // 生成验证码
        String text = kaptchaProducer.createText();
        BufferedImage image = kaptchaProducer.createImage(text);

        // 将验证码存入session
        session.setAttribute("kaptcha", text);

        // 将突图片输出给浏览器
        response.setContentType("image/png");
            OutputStream os = response.getOutputStream();
            ImageIO.write(image, "png", os);
    }

    @ApiOperation(value = "图片空间管理")
    @PostMapping("/bucketimages")
    @ResponseBody
    public DataResult getBucketsImagesList(@RequestBody Map<String,Object> map){
        String bucketname=map.get("bucketname").toString();
        List<Map<String, String>> resultmaplist=qiniuService.getArticleByImagename(bucketname);
        return DataResult.success(resultmaplist);
    }


    @ApiOperation(value = "删除七牛云图片")
    @PostMapping("/deletebucketimage")
    @ResponseBody
    public DataResult deletesImage(@RequestBody Map<String,Object> map){
        String key=map.get("imagekey").toString();
        String bucketname=map.get("bucketname").toString();
        Boolean b=qiniuService.deleteImageByKey(key,bucketname);
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(3001001,"七牛云图片删除失败");
        }
    }



}
